package com.palmshop.online.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dom4j.DocumentException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmshop.online.alipay.service.AlipayService;
import com.palmshop.online.dao.CusDao;
import com.palmshop.online.dao.ShopInfoDao;
import com.palmshop.online.entity.CusPoints;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.service.PayService;
import com.palmshop.online.wechat.service.WechatService;


/**
 * 支付service的实现类
 * @author quan
 *
 */
@Service
@Transactional
public class PayServiceImpl implements PayService {

	@Resource
	private ShopInfoDao dao;
	
	@Resource
	private CusDao dao2;

	/**
	 * 支付宝支付
	 * @return
	 */
	public String addBasket(Double payMoney,String saleListAddress,String cusUnique,String shopUnique,
			String saleListUnique,String shoppingCartId,String goodsCount,String goodsBarcodes){
		
		Map<String, String> params = new HashMap<String, String>();
		//根据商铺的标识查询出商铺的帐号
		List<Map<String,String>>list=dao.findByshopUnique(shopUnique);
		if(list!=null&&list.size()>0){
			params=list.get(0);
		}
		
		/**
		 * 支付成功后对订单状态以及购物车信息处理（暂时保留此处）
		 */
		dao.updateSaleById(saleListUnique);
		String ids[]=shoppingCartId.split(";");
		String goodsCounts[]=goodsCount.split(";");
		String goodsBarcode[]=goodsBarcodes.split(";");
		for(int i=0;i<ids.length;i++){//将对应库存数量减掉
			System.out.println(goodsCounts[i]+";"+goodsBarcode[i]);
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("goodsCount", Integer.parseInt(goodsCounts[i]));
			maps.put("goodsBarcode", goodsBarcode[i]);
			dao.batchStock(maps);
		}
		
		//支付主体**************************************************
		
		String sHtmlText = null;
		// 订单名称
		String subject = "";
		// 商户订单号
		String out_trade_no = "";
		// 付款金额
		String total_fee = "";
		// 商品展示地址
		// String show_url = "http://wx.vargo.com.cn/cn";
		// 订单描述
		String body = "";

						out_trade_no ="1605161113481348000020";
					
					Map<String, String> sParaTemp = new HashMap<String, String>();
					
					// 订单名称
					sParaTemp.put("subject", "交易付款");
					// 商户订单号
					sParaTemp.put("out_trade_no", saleListUnique);
					sParaTemp.put("shopping_cart_id", shoppingCartId);
					
					
					// 付款金额
					sParaTemp.put("total_fee","0.01");
//					sParaTemp.put("total_fee", payMoney.toString());
					// 订单描述
					sParaTemp.put("body", "");

					try {
						sHtmlText = AlipayService.create_direct_pay_by_user(sParaTemp,params);

					} catch (DocumentException | IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
		//支付主体**************************************************			
		
		return sHtmlText;
	}

	/**
	 * 微信支付确认
	 */
	@Override
	public String wechatPay() {
		String sHtmlText = null;
			try {
				sHtmlText =WechatService.weixin_pay() ;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		
		return sHtmlText;
	}
	

	
	/**
	 * 货到付款
	 * @param payMoney：支付金额
	 * @param saleListAddress：送货地址
	 * @param cusUnique：用户标识
	 * @param shopUnique：商铺标识
	 * @param saleListUnique：订单标识
	 * @param shoppingCartId：购物车标识
	 * @param goodsCount:商品数量
	 * @param goodsBarcodes：商品条形码
	 * @return result
	 */
	public PalmResult cashPay(Double payMoney,String saleListAddress,String cusUnique,String shopUnique,
					String saleListUnique,String shoppingCartId,String goodsCount,String goodsBarcodes){
		PalmResult result=new PalmResult();
		
		int state=0;
		if("119".equals(saleListAddress)){
			state=1;//游客身份直接购买
		}
		
		String ids[]=shoppingCartId.split(";");
		String goodsCounts[]=goodsCount.split(";");
		String goodsBarcode[]=goodsBarcodes.split(";");
		
		
		List<String>list=Arrays.asList(goodsBarcode);//将商品条码的数组转为集合list
		
		
		// 修改用户的积分信息
		if (state != 1) {
			int countPoints=dao.findPointsByGoods(shopUnique, list);//根据店铺标识和商品条码查询出所有商品的 积分

			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("cusUnique", cusUnique);
			map1.put("shopUnique", shopUnique);
			List<Map<String, Object>> list1 = dao2.findPointsExistById(map1);// 根据用户标识和商铺标识查询
																				// 用户积分表是否有记录
			if (list1 != null && list1.size() > 0) {
				Integer cusPointsId = (Integer) list1.get(0).get("cus_points_id");
				Integer cusShopPoints = (Integer) list1.get(0).get("cus_shop_points");
				Map<String, Object> map4 = new HashMap<String, Object>();
				map4.put("cusPointsId", cusPointsId);
				map4.put("cusShopPoints", cusShopPoints + countPoints);
				dao2.updatePointsById(map4);

			} else {
				CusPoints points = new CusPoints();
				points.setCusShopPoints(countPoints);
				points.setCusUnique(cusUnique);
				points.setShopUnique(shopUnique);
				dao2.addPoints(points);
			}
		}
		
		
		//将对应库存数量减掉
		for(int i=0;i<ids.length;i++){
			System.out.println(goodsCounts[i]+";"+goodsBarcode[i]);
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("goodsCount", Integer.parseInt(goodsCounts[i]));
			maps.put("goodsBarcode", goodsBarcode[i]);
			maps.put("shopUnique", shopUnique);
			dao.batchStock(maps);
		}
		
		//更改订单的状态
//		dao.deleteShopCart(ids);
		Map<String,Object>map2=new HashMap<String,Object>();
		
		map2.put("saleListUnique", saleListUnique);
		map2.put("saleListState", 1);
		map2.put("saleListHandlestate", 2);
		map2.put("saleListPayment", 1);
		dao.updateSaleById2(map2);
		result.setStatus(1);
		return result;
	}
	
	
	
	

}
